package com.uit.smarttool.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.uit.smarttool.dto.DanhSachSanPham;
import com.uit.smarttool.dto.KhachHang;
import com.uit.smarttool.ultility.CallSoap;


public final class LoginActivity extends ProgressBarActivity{

	public static KhachHang user_info;
	public static int MaGH;
	public static String user;
	public static String pass;
	public static final String PREFS_NAME = "MyPrefsFile";
	private static final String PREF_USERNAME = "username";
	private static final String PREF_PASSWORD = "password";
	
	public static String[] lvDanhmucsp;
	public static String[] lvImageCategory;
	public static List<DanhSachSanPham> tbDanhmucsanpham;
	public static String[] temp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);		
		this.setContentView(R.layout.login);
		
		title = "Vui lòng đợi...";
		
		final EditText e_user = (EditText) findViewById(R.id.edit_text_user);
		final EditText e_pass = (EditText) findViewById(R.id.edit_text_pass);
		
		final CheckBox checkBox = (CheckBox) findViewById(R.id.check_box_remember);
		
		
		SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
		String username = pref.getString(PREF_USERNAME, null);
		String password = pref.getString(PREF_PASSWORD, null);

		if (username != null && password != null) {
		    e_user.setText(username, TextView.BufferType.EDITABLE);
		    e_pass.setText(password, TextView.BufferType.EDITABLE);
		    checkBox.setChecked(true);
		}
						
		Button login = (Button) findViewById(R.id.button_login);
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub				
					if(!e_user.getText().toString().equals("") && !e_pass.getText().equals(""))
					{					
						user = e_user.getText().toString();
						pass = e_pass.getText().toString();
																		
						if(checkBox.isChecked())
						{
							getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
															        .edit()
															        .putString(PREF_USERNAME, user)
															        .putString(PREF_PASSWORD, pass)
															        .commit();
						}		
						else
						{
							getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
																	.edit()
																	.clear()
																	.commit();
						}
						addprogressbar(v.getContext());						
					}
					else
					{
						Toast.makeText(getApplicationContext(), "Xin điền đầy đủ thông tin", 2000).show();
					}				
			}
		});
		
		Button register = (Button) findViewById(R.id.button_register);
		register.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(v.getContext(), RegisterActivity.class);
				startActivityForResult(myIntent, 0);
			}
		});
		
		Button forget_pass = (Button) findViewById(R.id.button_forgetpass);
		forget_pass.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(v.getContext(), ResetPassActivity.class);
				startActivity(myIntent);
			}
		});
	}
	
	@Override
	public void _doWork()
	{
		CallSoap cs	= new CallSoap();		

		user_info = cs.selectCustomerByUser(user, pass);		
		
		if(user_info == null)
		{
			message = "Login failed. Please try again.";
			progressBarHandler.sendMessage(msg);
		}
		else
		{
			tbDanhmucsanpham = cs.getDanhMucSanPham();	
			
			lvImageCategory = new String[tbDanhmucsanpham.size()];
			lvDanhmucsp = new String[tbDanhmucsanpham.size()];
			temp = new String[tbDanhmucsanpham.size()];
			for (int i = 0; i < tbDanhmucsanpham.size(); i++)
			{					
				lvImageCategory[i] = tbDanhmucsanpham.get(i).getUrl();
				lvDanhmucsp[i] = tbDanhmucsanpham.get(i).getTenLoaiSP();
				temp[i] = "";
			}
			
			MaGH = cs.checkGioHang(user_info.getMakh());
			if (MaGH==0){
				MaGH = cs.getGioHangIDMax();
				MaGH = MaGH + 1;
				cs.insertGioHang(String.valueOf(MaGH), getDateNow(), 0, String.valueOf(user_info.getMakh()), 0);
				cs.insertCTGioHang(String.valueOf(MaGH), "1", 1);
			}else if(MaGH!=0){
				MaGH=MaGH;
			}
			this.finish();
			Intent myintent = new Intent(getApplicationContext(), MainActivity.class);
			startActivityForResult(myintent,0);
		}
	}
	public String getDateNow() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		return dateFormat.format(cal.getTime());
	}
	
	
}
